/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.dialog.xpath.model;

import java.net.URL;
import offset.nodes.client.model.ServerModel;
import offset.nodes.client.dialog.newnode.model.AbstractNewNodeConfigurator;
import offset.nodes.client.dialog.newnode.model.NewNodeConfigurator;
import offset.nodes.client.dialog.newnode.model.ObjectType;
import offset.nodes.client.dialog.newnode.model.ServiceProcessor;
import offset.nodes.client.dialog.newnode.model.Step;
import offset.nodes.client.dialog.xpath.view.QueryNamePanel;
import offset.nodes.client.xpath.view.XPathPanel;

/**
 *
 * @author Walter Lütgenau
 */
public class XPathNewNodeConfigurator extends AbstractNewNodeConfigurator implements NewNodeConfigurator {

    @Override
    public ObjectType getObjectType() {
        return new ObjectType(getBundle(), "contentType.xpathSearch.name", "contentType.xpathSearch.description",
                new Step[]{
                    new Step(getBundle(), "xpathSearch.step1.name", new XPathPanel(new ServerModel(getService()))),
                    new Step(getBundle(), "xpathSearch.step2.name", new QueryNamePanel())}, new ServiceProcessor(getService(), getPath()) {

            public URL process() {
                try {
                    XPathPanel xpathPanel = (XPathPanel) getObjectType().getSteps()[1].getPanel();
                    QueryNamePanel namePanel = (QueryNamePanel) getObjectType().getSteps()[2].getPanel();

                    NewXPathSearch.Request request = new NewXPathSearch.Request();
                    request.setParent(getPath());
                    request.setName(namePanel.getQueryName());
                    request.setXpathQuery(xpathPanel.getXPathQuery());

                    ServerModel model = new ServerModel(getService());
                    model.sendRequest(request);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return null;
            }
        });

    }
}
